/**
 * @note 地图量算
 * @author 吴义迪
 * @create_time 2016-12-24
 */

//变量定义
Global.measure = {
	labelYOffset1: 10,
	lengthClickFlag: true,
	areaClickFlag: true,
	lengthFlag: true,
	areaFlag: true,
	lengthIndex: null,
	areaIndex: null,
	lengthControl: new SuperMap.Control.Measure(SuperMap.Handler.Path, {immediate:true, geodesic:true}),
	areaControl: new SuperMap.Control.Measure(SuperMap.Handler.Polygon, {immediate:true, geodesic:true}),
	lengthLayer: new SuperMap.Layer.Vector("测距-点和文字"),
	lengthLayerLine: new SuperMap.Layer.Vector("测距-线"),
	lengthLayerX: new SuperMap.Layer.Vector("测距-删除"),
	lengthLayerTmp: new SuperMap.Layer.Vector("测距-临时")
};

/**
 * @note 长度量算
 * @author 吴义迪
 * @create_time 2016-12-24
 */
function mapLengthMeasure(){
	if(Global.measure.lengthClickFlag){
		Global.measure.lengthClickFlag = false;
		if(Global.measure.lengthFlag){
			Global.measure.lengthFlag = false;
		    //监听 measure 和 measurepartial 两个事件，量算完成时触发时
		    //量算完成时触发 measure 事件，当点被添加到量算过程中时触发 measurepartial
			Global.measure.lengthControl.events.on({
		        "measure": lengthHandleMeasure,
		        "measurepartial": lengthHandleMeasurements
		    });
		    //添加控件到 map 上
		    Global.common.map.addControl(Global.measure.lengthControl);
		}
		var flag = true;
		for(var i=0; i<Global.common.map.layers.length; i++){
			if(Global.common.map.layers[i].id == Global.measure.lengthLayer.id){
				flag = false;
				break;
			}
		}
		if(flag){
			Global.common.map.addLayers([Global.measure.lengthLayer, Global.measure.lengthLayerLine, Global.measure.lengthLayerX, Global.measure.lengthLayerTmp]);
			//将新的图层加到selectFeature控件中（刷新作用的图层）
	    	Global.common.selectFeatureLayers.push(Global.measure.lengthLayerX);
	    	Global.common.selectFeature.setLayer(Global.common.selectFeatureLayers);
		}
		Global.measure.lengthIndex = "" + new Date().getMonth() + new Date().getDay() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + new Date().getMilliseconds();
		Global.measure.lengthControl.activate();
	}
}

/**
 * @note 面积量算
 * @author 吴义迪
 * @create_time 2016-12-24
 */
function mapAreaMeasure(){
	if(Global.measure.areaClickFlag){
		Global.measure.areaClickFlag = false;
		if(Global.measure.areaFlag){
			Global.measure.areaFlag = false;
		    //监听 measure 和 measurepartial 两个事件，量算完成时触发时
		    //量算完成时触发 measure 事件，当点被添加到量算过程中时触发 measurepartial
			Global.measure.areaControl.events.on({
		        "measure": areaHandleMeasure
		    });
		    //添加控件到 map 上
		    Global.common.map.addControl(Global.measure.areaControl);
		}
		var flag = true;
		for(var i=0; i<Global.common.map.layers.length; i++){
			if(Global.common.map.layers[i].id == Global.measure.lengthLayer.id){
				flag = false;
				break;
			}
		}
		if(flag){
			Global.common.map.addLayers([Global.measure.lengthLayer, Global.measure.lengthLayerLine, Global.measure.lengthLayerX, Global.measure.lengthLayerTmp]);
			//将新的图层加到selectFeature控件中（刷新作用的图层）
	    	Global.common.selectFeatureLayers.push(Global.measure.lengthLayerX);
	    	Global.common.selectFeature.setLayer(Global.common.selectFeatureLayers);
		}
		Global.measure.areaIndex = "" + new Date().getMonth() + new Date().getDay() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + new Date().getMilliseconds();
		Global.measure.areaControl.activate();
	}
}

/**
 * @note 测距-最终双击结束
 * @author 吴义迪
 * @create_time 2016-12-24
 */
function lengthHandleMeasure(event){
	Global.measure.lengthControl.deactivate();
	Global.measure.lengthLayerTmp.removeAllFeatures();
    //获取当前坐标点
    var pointStart = new SuperMap.Geometry.Point(event.geometry.components[(event.geometry.components.length-1)].x, event.geometry.components[(event.geometry.components.length-1)].y);
    //克隆坐标点 用于显示关闭按钮
    var pointCopy = pointStart.clone();
	//单位转换
	var unitString = "";
	if(event.units == "m") {
		unitString = "米";
	}else if(event.units == "km") {
		unitString = "公里";
	}else{
		unitString = "";
	}
    var f = new SuperMap.Feature.Vector;
    f.geometry = pointStart;
    f.style= {
    	label: "总长：" + event.measure.toFixed(1) + unitString,
        fontColor: "red",
        fontWeight: "bold",
        strokeColor: "red",
        fontSize: "13px",
        strokeOpacity: 1,
        fillColor: "#ffffff",
        labelAlign: "lt",
        labelXOffset: 30,
        labelYOffset: Global.measure.labelYOffset1,
        pointRadius: 4
    };
    f.info = {
    	index: Global.measure.lengthIndex
    };
    //删除按钮
    var fc = new SuperMap.Feature.Vector;
    fc.geometry = pointCopy;
    fc.style= {
        externalGraphic: "resources/images/measureExit.png",
        strokeColor: "red",
        graphicWidth:16,
        graphicHeight:16,
        strokeOpacity: 1,
        fillColor: "#ffffff",
        graphicXOffset:10,
        pointRadius: 4
    };
    //保存之前绘制的节点用于显示其他样式
    var points = [];
    for(var i = 0;i < event.geometry.components.length; i++) {
        points.push(new SuperMap.Geometry.Point(event.geometry.components[i].x, event.geometry.components[i].y));
    }
    var qu = new SuperMap.Geometry.LineString(points);
    //保留线样式
    var ff = new SuperMap.Feature.Vector;
    ff.geometry = qu;
    ff.style = {
    	fill: true,
        strokeColor: "#FC854D",
        strokeWidth: 2.5,
        pointerEvents: "visiblePainted",
        fillColor: "#FC854D",
        fillOpacity: 1
    };
    ff.info = {
    	index: Global.measure.lengthIndex
    };
    fc.info = {
    	layerId: Global.measure.lengthLayerX.id,	//图层id
    	index: Global.measure.lengthIndex
    };
    Global.measure.lengthLayer.addFeatures(f);
    Global.measure.lengthLayerLine.addFeatures(ff);
    Global.measure.lengthLayerX.addFeatures(fc);
    Global.measure.lengthClickFlag = true;
}

/**
 * @note 测距-绘制过程中单机和移动事件
 * @author 吴义迪
 * @create_time 2016-12-24
 */
function lengthHandleMeasurements(event){
    //获取传入参数 event 的 geometry 信息
    var type = event.type;
	//单位转换
	var unitString = "";
	if(event.units == "m") {
		unitString = "米";
	}else if(event.units == "km") {
		unitString = "公里";
	}else{
		unitString = "";
	}
    if(type == "click") {
        //当==2时为起点
        if(event.geometry.components.length == 2) {
            var pointStart = new SuperMap.Geometry.Point(event.geometry.components[(event.geometry.components.length-1)].x,event.geometry.components[0].y);
            var f = new SuperMap.Feature.Vector;
            f.geometry = pointStart;
            f.style= {
                label: "起点",
                fontColor: "red",
                fontSize: "13px",
                strokeColor: "red",
                strokeOpacity: 1,
                fillColor: "#ffffff",
                labelXOffset: 3,
                labelYOffset: 18,
                pointRadius: 4
            };
            f.info = {
            	index: Global.measure.lengthIndex
            };
            Global.measure.lengthLayer.addFeatures(f);
        }else {
            //单机节点
            var pointStart = new SuperMap.Geometry.Point(event.geometry.components[(event.geometry.components.length-1)].x,event.geometry.components[(event.geometry.components.length-1)].y);
            var f = new SuperMap.Feature.Vector;
            f.geometry = pointStart;
            f.style= {
                label: + event.measure.toFixed(1)+unitString,
                fontColor: "red",
                fontSize: "13px",
                strokeColor: "red",
                strokeOpacity: 1,
                fillColor: "#ffffff",
                labelXOffset: 20,
                labelYOffset: 20,
                pointRadius: 4
            };
            f.info = {
            	index: Global.measure.lengthIndex
            };
            Global.measure.lengthLayer.addFeatures(f);
        }
    }else {
        //临时显示点信息
    	Global.measure.lengthLayerTmp.removeAllFeatures();
        var pointStart = new SuperMap.Geometry.Point(event.geometry.components[(event.geometry.components.length-1)].x,event.geometry.components[(event.geometry.components.length-1)].y);
        var f = new SuperMap.Feature.Vector;
        f.geometry = pointStart;
        f.style= {
            label: + event.measure.toFixed(1) + unitString,
            fontColor: "red",
            fontSize: "13px",
            strokeColor: "red",
            strokeOpacity: 1,
            fillColor: "#ffffff",
            labelXOffset: 20,
            labelYOffset: 20,
            pointRadius: 4
        };
        Global.measure.lengthLayerTmp.addFeatures(f);
    }
}

/**
 * @note 测面-最终双击结束
 * @author 吴义迪
 * @create_time 2016-12-24
 */
function areaHandleMeasure(event){
	Global.measure.areaControl.deactivate();
	Global.measure.lengthLayerTmp.removeAllFeatures();
    //获取当前坐标点
    var pointStart = new SuperMap.Geometry.Point(event.geometry.components[0].components[event.geometry.components[0].components.length-2].x, event.geometry.components[0].components[event.geometry.components[0].components.length-2].y);
    //克隆坐标点 用于显示关闭按钮
    var pointCopy = pointStart.clone();
	//单位转换
	var unitString = "";
	if(event.units == "m") {
		unitString = "平方米";
	}else if(event.units == "km") {
		unitString = "平方公里";
	}else{
		unitString = "";
	}
    var f = new SuperMap.Feature.Vector;
    f.geometry = pointStart;
    f.style= {
    	label: "总面积：" + event.measure.toFixed(1) + unitString,
        fontColor: "red",
        fontWeight: "bold", 
        fontSize: "13px",
        strokeColor: "red",
        strokeOpacity: 1,
        fillColor: "#ffffff",
        labelAlign: "lt",
        labelXOffset: 30,
        labelYOffset: Global.measure.labelYOffset1,
        pointRadius: 4
    };
    f.info = {
    	index: Global.measure.areaIndex
    };
    //删除按钮
    var fc = new SuperMap.Feature.Vector;
    fc.geometry = pointCopy;
    fc.style= {
        externalGraphic: "resources/images/measureExit.png",
        strokeColor: "red",
        graphicWidth:16,
        graphicHeight:16,
        strokeOpacity: 1,
        fillColor: "#ffffff",
        graphicXOffset:10,
        pointRadius: 4
    };
    //保存之前绘制的节点用于显示其他样式
    var points = [];
    for(var i = 0; i < event.geometry.components.length; i++) {
        points.push(new SuperMap.Geometry.Point(event.geometry.components[i].x, event.geometry.components[i].y));
    }
    var qu = new SuperMap.Geometry.LinearRing(points);
    //保留线样式
    var ff = new SuperMap.Feature.Vector(event.geometry.components[0]);
    ff.style = {
    	fill: true,
        strokeColor: "#FC854D",
        strokeWidth: 1,
        pointerEvents: "visiblePainted",
        fillColor: "#FC854D",
        fillOpacity: 0.4
    };
    ff.info = {
    	index: Global.measure.areaIndex
    };
    fc.info = {
    	layerId: Global.measure.lengthLayerX.id,	//图层id
    	index: Global.measure.areaIndex
    };
    Global.measure.lengthLayer.addFeatures(f);
    Global.measure.lengthLayerLine.addFeatures(ff);
    Global.measure.lengthLayerX.addFeatures(fc);
    Global.measure.areaClickFlag = true;
}

/**
 * @note 清除测量feature
 * @param f：删除的feature
 * @author 吴义迪
 * @create_time 2017-01-17
 */
function removeMeasureFeature(f){
	var f1 = [];
	var f2 = [];
	var f3 = [];
	for(var i=0; i<Global.measure.lengthLayer.features.length; i++){
		if(f.info.index == Global.measure.lengthLayer.features[i].info.index){
			f1.push(Global.measure.lengthLayer.features[i]);
		}
	}
	for(var i=0; i<Global.measure.lengthLayerLine.features.length; i++){
		if(f.info.index == Global.measure.lengthLayerLine.features[i].info.index){
			f2.push(Global.measure.lengthLayerLine.features[i]);
		}
	}
	for(var i=0; i<Global.measure.lengthLayerX.features.length; i++){
		if(f.info.index == Global.measure.lengthLayerX.features[i].info.index){
			f3.push(Global.measure.lengthLayerX.features[i]);
		}
	}
	Global.measure.lengthLayer.removeFeatures(f1);
    Global.measure.lengthLayerLine.removeFeatures(f2);
    Global.measure.lengthLayerX.removeFeatures(f3);
    if(Global.measure.lengthLayer.features.length == 0){
    	Global.common.map.removeLayer(Global.measure.lengthLayer);
    	Global.common.map.removeLayer(Global.measure.lengthLayerLine);
    	Global.common.map.removeLayer(Global.measure.lengthLayerX);
    	Global.common.map.removeLayer(Global.measure.lengthLayerTmp);
    	removeSelectFeatureLayersByName(Global.measure.lengthLayer.name);
    	removeSelectFeatureLayersByName(Global.measure.lengthLayerLine.name);
    	removeSelectFeatureLayersByName(Global.measure.lengthLayerX.name);
    	removeSelectFeatureLayersByName(Global.measure.lengthLayerTmp.name);
    }
}